<?php include_once("../../../common/common_setting_unit.php");         // 引入通用的设置文件 ?>
<?php include_once(ROOT_PATH_ . "/controller/common/module/head.php"); // 引入通用的头部 ?>



<?
//设置各图片的默认值,如果没有上传，都会是记录透明的图片
$icon = "alpha.gif";
$banner = "alpha.gif";
$extend_img = "alpha.gif";
?>


<?
//设置需要搜索的表格的前缀后缀
$table_Suffix = "term";
$table        = $table_Prefix . $table_Suffix;
$sort_by      = "nid";
$sort_order   = "desc";
//设置需要搜索的表格的排序项和升降项
?>





<?php

$brand_list_json = json_decode($_REQUEST['brand_list_json'],true);  //关联文章信息处理

$new_brand_list = '';

if($_REQUEST['brand_list_json'] != '[]'){
	$brand_list_arr = array();
	foreach($brand_list_json as $brand_val){
		
		if(!empty($brand_val['children'])){
			$brand_list_arr [] = get_deep_id($brand_val['children']);
		}
		else{
			$brand_list_arr [] = $brand_val['id'];
		}
		
	}
	static $res_arr = array();
	$new_brand_list = toOneArray($brand_list_arr,true);
}




function toOneArray($array,$clear=false)
{
    static $res_arr = array();
	if ($clear)
    {
        $res_arr = array();
    }
    foreach ($array as $v) {
        if (is_array($v)) {
            toOneArray($v);
        } else {
            $res_arr[] = $v;
        }
    }
    return $res_arr;
}

function get_deep_id($list){
	$id_arr = array();
	foreach($list as $val){
		if(!empty($val['children'])){
			return  get_deep_id($val['children']);
		}
		
		$id_arr[] = $val['id'];
		
	}
	
	return $id_arr;
}



//这里是设计上传文件的部分：包含图片和附件。视频也应该用这种形式上传
//统一设置一下保存图片的路径
$_uf['path']        = ROOT_PATH_ . "/uploadfiles/".$theme_folder."/";





//上传icon
if (is_uploaded_file($_FILES['icon']['tmp_name']))
{
    $_uf['upfile']       = $_FILES['icon'];
    $_info               = upload_file($_uf);
    $_REQUEST['ai_icon'] = $_info['name'];
    $icon          = $_REQUEST['ai_icon'];


}

//上传Banner
if (is_uploaded_file($_FILES['banner']['tmp_name']))
{
    $_uf['upfile']       = $_FILES['banner'];
    $_info               = upload_file($_uf);
    $_REQUEST['ai_banner'] = $_info['name'];
    $banner        = $_REQUEST['ai_banner'];

}

//
if (is_uploaded_file($_FILES['extend_img']['tmp_name']))
{
    $_uf['upfile']       = $_FILES['extend_img'];
    $_info               = upload_file($_uf);
    $_REQUEST['ai_extend_img'] = $_info['name'];
    $extend_img        = $_REQUEST['ai_extend_img'];

}


?>





<?php

if(!$_REQUEST['term_is_disabled']){$_REQUEST['term_is_disabled']="0";}?>

<?php


$new_term_mid           =addslashes($_REQUEST['term_mid']);
$new_term_name          =addslashes($_REQUEST['term_name']);
$new_term_en_name          =addslashes($_REQUEST['term_en_name']);
$new_term_title         =addslashes($_REQUEST['term_title']);
$new_term_keyword       =addslashes($_REQUEST['term_keyword']);
$new_term_description   =addslashes($_REQUEST['term_description']);
$new_term_url           =addslashes($_REQUEST['term_url']);
$new_term_introduce     =addslashes($_REQUEST['term_introduce']);
$new_term_name          =addslashes($_REQUEST['term_name']);
$new_term_detail        =addslashes($_REQUEST['term_detail']);
$new_term_discountprice =addslashes($_REQUEST['term_discountprice']);
$new_term_listorder     =addslashes($_REQUEST['term_listorder']);
$new_term_hotsale       =addslashes($_REQUEST['term_hotsale']);
$new_term_recommend     =addslashes($_REQUEST['term_recommend']);
$new_term_publish       =addslashes($_REQUEST['term_publish']);
$new_term_clickrate     =addslashes($_REQUEST['term_clickrate']);

$new_term_modifytime    =addslashes($_REQUEST['term_modifytime']);


$new_term_id            =addslashes($_REQUEST['term_term_id']);
$new_term_link          =addslashes($_REQUEST['term_link']);
$new_term_mark          =addslashes($_REQUEST['term_mark']);
$new_term_markorder     =addslashes($_REQUEST['term_markorder']);
$new_term_vip           =addslashes($_REQUEST['term_vip']);
$new_term_is_disabled   =addslashes($_REQUEST['term_is_disabled']);

$new_term_banner_title =addslashes($_REQUEST['term_banner_title']);
$new_term_banner_url =addslashes($_REQUEST['term_banner_url']);



?>





<?php
//此段函数，根据获取过来的id，查询出所属分类的term1~term5，interm
if ($new_term_id == 'root') {
    $trans_term_term0 = 0;
    $trans_term_interm = 'root';

} else {


    $result = mysql_query("select * from $table where nid = $new_term_id");
//根据ID,获取相应的所属分类资料
    while ($row = mysql_fetch_array($result)) {

        $trans_term_nid = $new_term_id;
        $trans_term_term0 = $row['term0'];
        $trans_term_term1 = $row['term1'];
        $trans_term_term2 = $row['term2'];
        $trans_term_term3 = $row['term3'];
        $trans_term_term4 = $row['term4'];
        $trans_term_term5 = $row['term5'];
        $trans_term_interm = $row['interm'];
    }
//下面这个判断，选择root没问题，其他的if else要修改一下
    if ($trans_term_interm == "root") {
        $trans_term_term1 = $new_term_id;
        $trans_term_interm = "term1";
    } else if ($trans_term_interm == "term1") {
        $trans_term_term2 = $trans_term_nid;
        $trans_term_interm = "term2";
    } else if ($trans_term_interm == "term2") {
        $trans_term_term3 = $trans_term_nid;
        $trans_term_interm = "term3";
    } else if ($trans_term_interm == "term3") {
        $trans_term_term4 = $trans_term_nid;
        $trans_term_interm = "term4";
    } else if ($trans_term_interm == "term4") {
        $trans_term_term5 = $trans_term_nid;
        $trans_term_interm = "term5";
    }


}

mysql_close($con);

?>



<?


$sql = "INSERT INTO $table (
mid,
title,
name,
en_name,
keyword,
description,
url,
introduce,
detail,
hotsale,
recommend,
listorder,
clickrate,
modifytime,
publish,
term_banner,
term0,
term1,
term2,
term3,
term4,
term5,
interm,
icon,
link,
mark,
markorder,
belong,
extend_img,
vip_term,
is_disabled,
banner_title,
banner_url,
shop_brand

) 
value 
(
'$new_term_mid',
'$new_term_title',
'$new_term_name',
'$new_term_en_name',
'$new_term_keyword',
'$new_term_description',
'$new_term_url',
'$new_term_introduce',
'$new_term_detail',
'$new_term_hotsale',
'$new_term_recommend',
'$new_term_listorder',
'$new_term_clickrate',
'$new_term_modifytime',
'$new_term_publish',
'$banner',
'$trans_term_term0',
'$trans_term_term1',
'$trans_term_term2',
'$trans_term_term3',
'$trans_term_term4',
'$trans_term_term5',
'root',
'$icon',
'$new_term_link',
'$new_term_mark',
'$new_term_markorder',
'shop',
'$destination2',
'$new_term_vip',
'$new_term_is_disabled',
'$new_term_banner_title',
'$new_term_banner_url',
'".implode(',',$new_brand_list)."'
)";

sql_log($sql);
$query = mysql_query($sql) or die (mysql_error().'保存term表失败');

$new_term_nid = mysql_insert_id();

if($new_brand_list){
	//先查品牌下已经存在什么商品
	$sql = "select * from ".$table_Prefix."relation where term_nid in(".implode(',',$new_brand_list).") and belong = 'brand' group by detail_nid ";
	
	$res = mysql_query($sql);
 	
	$brand_goods = array();
	while($row = mysql_fetch_assoc($res))
    {
       $brand_goods[$row['nid']]=$row['detail_nid'];
    }
	
	if(!empty($brand_goods)){
		$insert_val = '';
		foreach($brand_goods as $val){
			
			if($insert_val != ''){
				$insert_val.= ',';
			}
			$insert_val.= "('$val','$new_term_nid', 'shop' , 0)";
		}
		
		
		$sql = "INSERT INTO ".$table_Prefix."relation (detail_nid , term_nid , belong , vip_term) VALUES $insert_val";
		
		sql_log($sql);
		$query = mysql_query($sql) or die ('此次数据库操作失败');
	}
}

mysql_close($con);

?>






<script type="text/javascript">

    alert("输入信息已保存，点击确认返回继续操作.");
    document.location.href = "term-list.php?sys_part=<? echo $system_part ?>";

</script>

</body>